package com.hunan.chinamobile.manager;


import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.chinamobile.core.BaseController;
import com.hunan.chinamobile.core.Result;
import com.hunan.chinamobile.core.ResultGenerator;
import com.hunan.chinamobile.dto.ExceptionDTO;
import com.hunan.chinamobile.service.ExceptionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/exception")
public class ExceptionController extends BaseController{

    @Resource
    private ExceptionService exceptionService;


    /**
     * 事件列表
     * @param request
     * @param response
     * @param cityId 城市ID
     * @param bsId 基站ID
     * @param type 事件类型，null-全部，1-电压，2-光纤
     * @param state 事件状态，null-全部，0-未恢复，1-已恢复
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public Result list(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam(required = false) String cityId,
                       @RequestParam(required = false) String bsId,
                       @RequestParam(required = false) Integer type,
                       @RequestParam(required = false) Integer state,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "size", defaultValue = "20") Integer size){

        if(StringUtils.isEmpty(cityId)){
            cityId = getCityId(request, response);
        }
        PageHelper.startPage(page,size);
        List<ExceptionDTO> exceptionDTOS = exceptionService.findList(cityId, bsId, type, state);
        PageInfo<ExceptionDTO> pageInfo = new PageInfo<>(exceptionDTOS);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
